package com.example.transferfile;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.apache.commons.net.ftp.*;

public class MainActivity extends Activity {

	EditText edFptSever, edPort, edUserName, edPassWord;
	Button btnLogin;
	public FTPClient mFTPClient = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		edFptSever = (EditText) findViewById(R.id.editText1);
		edPort = (EditText) findViewById(R.id.editText2);
		edUserName = (EditText) findViewById(R.id.editText3);
		edPassWord = (EditText) findViewById(R.id.editText4);
		btnLogin = (Button) findViewById(R.id.button1);
		
		btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String ftpSever = edFptSever.getText().toString();
				int port = Integer.parseInt(edPort.getText().toString());
				String userName = edUserName.getText().toString();
				String passWord = edPassWord.getText().toString();
				
			//	Toast.makeText(getApplicationContext(), "Thanh cong", Toast.LENGTH_LONG).show();
				ftpConnect(ftpSever, userName, passWord, port);
			}
		});
	}

	public boolean ftpConnect(String host, String username, String password,
			int port) {
		try {
			mFTPClient = new FTPClient();
			// connecting to the host
			mFTPClient.connect(host, port);

			// now check the reply code, if positive mean connection success
			if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
				// login using username & password
				boolean status = mFTPClient.login(username, password);

				mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
				mFTPClient.enterLocalPassiveMode();

				Intent i = new Intent(getApplicationContext(),listview.class);
				startActivity(i);
				finish();
				
				return status;
			}
		} catch (Exception e) {
			Log.d("Error: could not connect to host " , host);
		}

		return false;
	}
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
